/**
 * 
 */
package com.wsmhz.security.core.authentication;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wsmhz.security.core.properties.LoginResponseType;
import com.wsmhz.security.core.properties.SecurityProperties;
import com.wsmhz.security.core.utils.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

/**
 * create by tangbj on 2018/3/27
 * 自定义验证成功处理器
 */
@Component("wsmhzAuthenticationSuccessHandler")
public class WsmhzAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private SecurityProperties securityProperties;

	@Override
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
			Authentication authentication) throws IOException, ServletException {

			logger.info("登录成功");
			if(securityProperties.getBrowser().getLoginResponseType().equals(LoginResponseType.JSON)){
				response.setContentType("application/json;charset=UTF-8");
				response.getWriter().write(JsonUtil.objToString(authentication.getName()+":登陆成功,可重写返回值"));
			}else{
				super.onAuthenticationSuccess(request,response,authentication);
			}

		}

}
